package com.josn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.josn.mapper.ArticleMapper;
import com.josn.pojo.Article;
import com.josn.pojo.PageBean;
import com.josn.service.ArticleService;
import com.josn.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    //新增文章
    @Override
    public void add(Article article) {
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer create_user=(Integer) map.get("id");
        article.setCreate_user(create_user);
        articleMapper.add(article);
    }

    //条件分页列表查询
    @Override
    public PageBean<Article> list(Integer page_num, Integer page_size, Integer category_id, String state) {
        //1、创建PageBean对象
        PageBean<Article> pb=new PageBean<>();
        //2、开启分页查询
        PageHelper.startPage(page_num,page_size);
        //3、调用mapper
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer create_user=(Integer) map.get("id");
        List<Article> as=articleMapper.list(create_user,category_id,state);
        //4、Page中提供了方法，可以获取PageHealper分页查询后，得到的总记录条数和当前页数据
        Page<Article> p=(Page<Article>) as;
        //5、把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    //获取文章详情
    //更新文章
    //删除文章
}
